<template>
  <div class="addAccount">
    <el-button type="primary" size="small" @click="dialogFormVisible = true">添加商品</el-button>
    <el-dialog class="dialog" title="添加商品" :visible.sync="dialogFormVisible" :before-close="close">
      <el-form :model="form">
        <el-row>
          <el-col :span="10">
            <el-form-item label="商品名：" :label-width="formLabelWidth">
              <el-input v-model="form.name" auto-complete="off" placeholder="商品名" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="单价：" :label-width="formLabelWidth">
              <el-input v-model="form.price" auto-complete="off" placeholder="商品单价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="商品编号：" :label-width="formLabelWidth">
              <el-input v-model="form.number" auto-complete="off" placeholder="商品编号" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="商品类别：" :label-width="formLabelWidth">
              <el-select v-model="form.type" placeholder="商品类别" style="width:100%">
                <el-option label="区域一" value="shanghai" />
                <el-option label="区域二" value="beijing" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="库存：" :label-width="formLabelWidth">
              <el-input v-model="form.repertory" auto-complete="off" placeholder="商品库存" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="销量：" :label-width="formLabelWidth">
              <el-input v-model="form.amount" auto-complete="off" placeholder="商品销量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="进价：" :label-width="formLabelWidth">
              <el-input v-model="form.originalPrice" auto-complete="off" placeholder="商品进价" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="进货日期：" :label-width="formLabelWidth">
              <el-date-picker v-model="form.stockTime" type="date" placeholder="选择日期" style="width: 100%;" />
              <!-- <el-input v-model="form.stockTime" auto-complete="off" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="商品状态：" :label-width="formLabelWidth">
              <el-switch v-model="form.status" />
            </el-form-item>
          </el-col>
          <el-col :span="10" />
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="介绍：" :label-width="formLabelWidth">
              <el-input v-model="form.description" type="textarea" placeholder="商品简单介绍" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item class="headPortrait" label="商品图：" :label-width="formLabelWidth">
          <!-- <el-upload
            action="https://www.easy-mock.com/mock/5d6299a11fc89667465ec0e7/user/avatar"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload> -->
          <!-- <el-dialog :visible.sync="dialogVisible" size="tiny">
            <img width="100%" :src="imageUrl" alt="">
          </el-dialog> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="addGoods">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addGoods } from '@/api/goods'
import { allChinese, numAndWord, plusNum, positive } from '@/utils/validate.js'
export default {
  name: 'AddGood',
  data() {
    return {
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: '',
        number: '',
        type: [],
        price: '',
        amount: '',
        repertory: '',
        originalPrice: '',
        stockTime: '',
        description: '',
        status: false
      },
      imageUrl: '',
      formLabelWidth: '90px',
      formRegionWidth: '100%'
    }
  },
  methods: {
    addGoods() {
      const data = {
        number: this.form.number,
        name: this.form.name,
        type: this.form.type,
        price: this.form.price,
        amount: this.form.amount,
        repertory: this.form.repertory,
        originalPrice: this.form.originalPrice,
        stockTime: this.form.stockTime,
        description: this.form.description,
        status: this.form.status
      }
      console.log(data.status)
      if (!allChinese(data.name)) {
        this.$message.error('商品名需为中文或字母数字的组合！')
      } else if (!plusNum(data.price)) {
        this.$message.error('商品价格只能为数字！')
      } else if (!numAndWord(data.number)) {
        this.$message.error('商品编号应为数字和字母组合！')
      } else if (data.type.length === 0) {
        this.$message.error('请选择商品分类！')
      } else if (!positive(data.repertory)) {
        this.$message.error('库存只能为整数！')
      } else if (!positive(data.amount)) {
        this.$message.error('销量只能为整数！')
      } else if (!plusNum(data.originalPrice)) {
        this.$message.error('商品进价只能为数字！')
      } else if (data.stockTime.length === 0) {
        this.$message.error('请选择进货日期！')
      } else if (data.description.length === 0) {
        this.$message.error('请输入商品简介！')
      } else {
        addGoods(data).then(res => {
          console.log(res)
        })
      }
    },
    handleRemove(file, fileList) {
    },
    close() {
      this.dialogFormVisible = false
    }
  }
}
</script>

<style lang="stylus" scoped>
.dialog >>> .el-dialog
  width 60%
  .avatar-uploader >>> .el-upload
    border 1px dashed #d9d9d9
    border-radius 6px
    cursor pointer
    position relative
    overflow hidden
  .avatar-uploader >>> .el-upload:hover
    border-color #409EFF
  .addAccount >>> .regionSelect
    width 100%
.addAccount
  padding-left 20px
  padding-top 20px
  .avatar-uploader-icon
    font-size 28px
    color #8c939d
    text-align center
  .avatar
    width 178px
    height 178px
    display block
</style>
